<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Cat;

class CatController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //显示栏目页面
        $data = Cat::all();
  
        $cat = $this->findTr($data,0,0);


        return view('admin.cat_index',['cat'=>$cat]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //添加商品
            $data = Cat::all();
  
        $cat = $this->findTr($data,0,0);
   
        return view('admin.cat_add',['cat'=>$cat]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //添加商品信息入库

        $cat = new Cat();
        $cat->title = $request->title;
        $cat->catid = $request->catid;
        $cat->save();
        return redirect('admin/cat');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    //找子孙栏目
    protected function findTr($data,$id=0,$lev=0)
    {
            static $tree = [];
            foreach($data as $k=>$v){
                if($v['catid']==$id){
                    $v['lev'] = $lev;
                    $tree[] = $v;
                    $this->findTr($data,$v['id'],$lev+1);
                }
            }
            return $tree;
    }


}


